//
//  SWTabBar.m
//  新浪微博 OC版
//
//  Created by wszyxc on 16/7/18.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "SWTabBar.h"

@implementation SWTabBar


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        //设置子控件
        [self setupUI];
        
    }
    
    return self;
}


- (void)setupUI{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [self addSubview:btn];
    
    btn.center = self.center;
    
//    NSLog(@"%d",self.subviews.count);
    
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    //重新布局子控件

    
    CGFloat itemW = ScreenWidth/5;
    CGFloat itemH = self.height;
    CGFloat itemY = 0;
    CGFloat itemX = 0.0;
    
    NSInteger index = 0;
    
    //tabBar的子控件里面包含4个tabbarButton,一个button,一个backgroundView,一个imageView
    for (UIButton * item in self.subviews) {
        
        //判断是否是 uibutton或者uitabBarButton ,查看过他们都是继承自UIControl
        if ([item isKindOfClass:[UIControl class]]) {
            
            //如果不是uibutton
            if (![item isKindOfClass:[UIButton class]]) {
                
                itemX = itemW * index;
                
                index++;
                
                if (index == 2) {
                    index++;
                }
                
            }else{
                //如果是自己添加的button
                    itemX = itemW * 2;
            }
            
            item.frame = CGRectMake(itemX, itemY, itemW, itemH);
            
        }
      
    }
    
}



@end
